﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using ttlang;

namespace ttlang.builder
{
    static class ProjectExporter
    {


        /// <summary>
        /// 导出 tsconfig.json
        /// </summary>
        public static void ExportTSProjFile(TsBuilderContext option,string path, ProjectOption.ProjectType outputType)
        {
            
            var str = "";
            //导出路径
            var savePath = path  + "tsconfig.json";

            if (option.mode == BuildMode.System)
            {
                //module : system  是将目录下的所有.ts 文件 全部生成到 outFile所对应的路径 main.js文件中
                //         Commonjs 各自的.ts文件 生成各自对应的.js文件，生成的目标路径为 outDir
                //   ""outDir"": ""./ bin / "",
                //拼接
                //需要有个outDir,否则容易冲突
                str = @"{
    ""compilerOptions"": {
        ""module"": ""system"",
        ""target"": ""es5"",
        ""sourceMap"": false,
        ""outFile"": ""./bin/main.js"",
        ""outDir"":""./bin/"",
        ""removeComments"": true,
        ""declaration"": true,
        ""lib"": [
            ""dom"",
            ""es2015.promise"",
            ""es2015""
        ]
    }
}";
            }
            else
            {
                throw new Exception("not implement commonjs mode yet.");
            }

            //tsconfig.json 保存成文件

            if (System.IO.File.Exists(savePath))
                System.IO.File.Delete(savePath);
            System.IO.File.WriteAllBytes(savePath, Encoding.UTF8.GetBytes(str));
            //System.IO.File.WriteAllText(savePath, str);
        }


        public static void ExportTSProjHtml(string path)
        {
            var _indexHtml = System.IO.Path.Combine(path, "index.html");
            System.IO.File.WriteAllText(_indexHtml, indexHtml);
        }
        
        public static string indexHtml = @"
<!DOCTYPE html>
<html style=""height: 100%;"">
<head>
    <title>
    </title>
    <meta charset = ""utf-8"" />
   
    <script src = ""./bin/main.js"" ></script>
</head>
</html>";
     }
}
